Page({
  data: {
    dateList:[{"index":0,"name":"前一天"},{"index":1,"name":"今天"},{"index":2,"name":"近一周"},{"index":3,"name":"近一个月"}],
    dateType:0,
    deviceId:'',
    beginTime:'2024-05-29 00:00:00',
    endTime:'2024-05-29 00:00:00',
    chartData1: {},

    chartData2: {},
    //您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
    opts1: {},

    opts2: {},
  },
  onLoad(options){
      let id=options.id
      this.setData({deviceId:id})
  },
  // 选择日期类型
  selectType(e){
    let type=e.currentTarget.dataset.type
    this.setData({
      dateType:type
    })
    this.getServerData()
  },
  onReady() {

    const opts = {
      // color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
      padding: [15,10,0,15],
      dataLabel: false,
      dataPointShape: false,
      enableScroll: false,
      legend: { show: false },
      xAxis: {
        disableGrid: true,
        labelCount:5,
        rotateLabel:true
      },
      yAxis: {
        gridType: "dash",
        dashLength: 2,
        data: [{ min: 0}]
      },
      extra: {
        line: {
          type: "curve",
          width: 2,
          activeType: "hollow",
          linearType: "custom",
          onShadow: false,
          animation: "horizontal"
        }
      }
    }

    this.setData({
      opts1: opts, 
      opts2: opts, 
    })

    this.getServerData();
  },
  // 格式化日期yyyy-mm-dd
  formatDate(date) {
    date = new Date(date);
    let year=date.getFullYear()+"-"
    let month=(date.getMonth()<10?"0"+(date.getMonth() + 1):date.getMonth() + 1)+"-"
    let day=(date.getDate()<10?"0"+date.getDate():date.getDate())
    return year+month+day;
  },
  /**
   *  获取前几天的日期
   * @param {*} num 前几天
   */
   getBeforDay(num) {
    var currentDate = new Date(); // 获取当前日期
    var previousDate = new Date(currentDate.getTime() - (num*24 * 60 * 60 * 1000)); // 将当前日期的毫秒数减去一天的毫秒数
    return previousDate;
  },
  // 获取数据
    getServerData() {
      if(this.data.dateType==0){//前一天
        let start=this.getBeforDay(1);
        let end=this.getBeforDay(0);
        this.setData({
          beginTime:this.formatDate(start)+" 00:00:00",
          endTime:this.formatDate(end)+" 23:59:59",
        })
      }else if(this.data.dateType==1){//今天
        let start=this.getBeforDay(0);
        let end=this.getBeforDay(0);
        this.setData({
          beginTime:this.formatDate(start)+" 00:00:00",
          endTime:this.formatDate(end)+" 23:59:59",
        })
      }else if(this.data.dateType==2){//近7天
        let start=this.getBeforDay(6);
        let end=this.getBeforDay(0);
        this.setData({
          beginTime:this.formatDate(start)+" 00:00:00",
          endTime:this.formatDate(end)+" 23:59:59",
        })
      }else{//近30天
        let start=this.getBeforDay(30);
        let end=this.getBeforDay(0);
        this.setData({
          beginTime:this.formatDate(start)+" 00:00:00",
          endTime:this.formatDate(end)+" 23:59:59",
        })
      }
      // 获取温度数据
      wx.$req({
        url: '/wms/monitor/getMonitoringDataListByDeviceIdAndPointId?type=wd',
        // '&deviceId='+this.data.deviceId+'&beginTime='+this.data.beginTime+'&endTime'+this.data.endTime,
        method: 'post',
        data:{"deviceId":this.data.deviceId,beginTime:this.data.beginTime,endTime:this.data.endTime}
      }).then(res => {
        let list=res.data
        let categories=[]
        let data=[]
        for(let i=0;i<list.length;i++){
            let item=list[i];
            let time=item.createTime.substring(11,16)
            categories.push(time)
            data.push(parseFloat(item.pointValue))
        }
        let chardata = {
          categories: categories,
          series: [
            {
              name:"温度",
              linearColor: [  [  0.5,  "#00D1ED" ],  [  0.75, "#00E6BB"  ], [  1, "#90F489" ] ],
              setShadow: [ 3,  8,  10,  "#1890FF"],
              data: data
            },
          ]
        };

        this.setData({ 
          chartData1: JSON.parse(JSON.stringify(chardata)),
        });
      })

      // 获取湿度数据
      wx.$req({
        url: '/wms/monitor/getMonitoringDataListByDeviceIdAndPointId?type=sd',
        // '&deviceId='+this.data.deviceId+'&beginTime='+this.data.beginTime+'&endTime'+this.data.endTime,
        method: 'post',
        data:{"deviceId":this.data.deviceId,beginTime:this.data.beginTime,endTime:this.data.endTime}
      }).then(res => {
        let list=res.data
        let categories=[]
        let data=[]
        for(let i=0;i<list.length;i++){
            let item=list[i];
            let time=item.createTime.substring(11,16)
            categories.push(time)
            data.push(parseFloat(item.pointValue))
        }
        let chardata = {
          categories: categories,
          series: [
            {
              name:"湿度",
              linearColor: [  [  0.5,  "#00D1ED" ],  [  0.75, "#00E6BB"  ], [  1, "#90F489" ] ],
              setShadow: [ 3,  8,  10,  "#1890FF"],
              data: data
            },
          ]
        };

        this.setData({ 
          chartData2: JSON.parse(JSON.stringify(chardata)),
        });
      })

    },
 
})